package com.siro.pm.ui.main;

import java.util.List;

import com.siro.pm.I18nManager;
import com.siro.pm.model.DemandOrBug;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.DemandOrBugProvider;
import com.siro.pm.ui.detail.BugDetailPage;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since BugsPage.java v1.0
 */
public class BugsPage extends VerticalLayout {
	private static final long serialVersionUID = -6963599324304409783L;

	BugsPage self;
	private Project project;
	// private List<DemandOrBug> bugs;

	private Button button_AddBug;
	private Button button_DeleteBug;
	public Table demandOrBugsTable;

	public BugsPage() {
		self = this;
	}

	public BugsPage(Project project, List<DemandOrBug> bugs) {
		this.project = project;
		// this.bugs = bugs;
	}

	public void createContents() {
		setSizeFull();
		setSpacing(true);
		setMargin(true, true, true, true);

		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		// Add Bug
		button_AddBug = new Button(I18nManager.getMessage("button.add"));
		button_AddBug.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				BugDetailPage bugDetailPage = new BugDetailPage();
				bugDetailPage.setBugsPage(self);
				bugDetailPage.setOwnerProject(project);
				bugDetailPage.createContents();
				Window bugDetailWindow = new Window(I18nManager
						.getMessage("bug.info"));
				bugDetailWindow.setModal(true);
				bugDetailWindow.setWidth("800px");
				bugDetailWindow.setHeight("80%");
				bugDetailPage.setWindow(bugDetailWindow);
				bugDetailWindow.addComponent(bugDetailPage);
				getWindow().addWindow(bugDetailWindow);
			}
		});
		buttons.addComponent(button_AddBug);
		buttons.setComponentAlignment(button_AddBug, Alignment.TOP_RIGHT);

		// Delete Bug
		button_DeleteBug = new Button(I18nManager.getMessage("button.delete"));
		if (!Developer.isProjectManager(project.getManager())) {
			button_DeleteBug.setEnabled(false);
		}
		button_DeleteBug.addListener(new ClickListener() {
			private static final long serialVersionUID = -5620810406204435724L;

			public void buttonClick(ClickEvent event) {
				Object object = demandOrBugsTable.getValue();
				if (object != null) {
					DemandOrBug bug = (DemandOrBug) object;
					DemandOrBug.deleteDemandOrBug(bug);
					demandOrBugsTable.removeItem(bug);
				}
			}
		});
		buttons.addComponent(button_DeleteBug);
		buttons.setComponentAlignment(button_DeleteBug, Alignment.TOP_RIGHT);

		addComponent(buttons);
		setComponentAlignment(buttons, Alignment.TOP_RIGHT);

		// Bugs Table
		demandOrBugsTable = new Table();
		demandOrBugsTable.setWidth("100%");
		demandOrBugsTable.setHeight("100%");
		demandOrBugsTable.setSelectable(true);
		demandOrBugsTable.setColumnWidth(DemandOrBugProvider.BUG_PROPERTY_NAME,
				100);
		demandOrBugsTable.setColumnWidth(
				DemandOrBugProvider.BUG_PROPERTY_PROJECT, 100);
		demandOrBugsTable.setContainerDataSource(DemandOrBugProvider
				.getBugsData(project.getDemandsOrBugs()));
		demandOrBugsTable.setColumnReorderingAllowed(true);
		demandOrBugsTable.setColumnCollapsingAllowed(true);
		// Generate operations
		demandOrBugsTable.addGeneratedColumn(
				I18nManager.getMessage("operation"),
				new Table.ColumnGenerator() {
					private static final long serialVersionUID = -6031723727120033742L;

					public Component generateCell(Table source, Object itemId,
							Object columnId) {
						HorizontalLayout opts = new HorizontalLayout();
						DemandOrBug bug = (DemandOrBug) itemId;
						if (DemandOrBug.couldEditBug(bug)) {
							Button edit = new Button(I18nManager
									.getMessage("edit"));
							edit.addListener(new EditClickListener());
							opts.addComponent(edit);
						}
						return opts;
					}

				});
		addComponent(demandOrBugsTable);
		setExpandRatio(demandOrBugsTable, 1);

	}

	class EditClickListener implements ClickListener {
		private static final long serialVersionUID = -6928858438628311410L;

		public void buttonClick(ClickEvent event) {
			Object object = demandOrBugsTable.getValue();
			if (object != null) {
				DemandOrBug bug = (DemandOrBug) object;
				BugDetailPage bugDetailPage = new BugDetailPage();
				bugDetailPage.setBugsPage(self);
				bugDetailPage.setOwnerProject(project);
				bugDetailPage.setBug(bug);
				bugDetailPage.createContents();
				Window bugDetailWindow = new Window(
						I18nManager.getMessage("bug.info"));
				bugDetailWindow.setModal(true);
				bugDetailWindow.setWidth("800px");
				bugDetailWindow.setHeight("80%");
				bugDetailPage.setWindow(bugDetailWindow);
				bugDetailWindow.addComponent(bugDetailPage);
				getWindow().addWindow(bugDetailWindow);
			}

		}

	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	// public List<DemandOrBug> getBugs() {
	// return bugs;
	// }
	//
	// public void setBugs(List<DemandOrBug> bugs) {
	// this.bugs = bugs;
	// }

}
